<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad">
    </avue-crud>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/lamp/devicedata";
  import {mapGetters} from "vuex";

  export default {
    data() {
      return {
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
          option: {
              height:'auto',
              calcHeight: 350,
              tip: false,
              border: true,
              index: true,
              viewBtn: true,
              selection: true,
              addBtn: false,
              editBtn: false,
              delBtn: false,
              labelWidth: 120,
              menuWidth: 120,
              column: [
                  {
                      label: "网关IMEI",
                      prop: "imei",
                      search: true,
                      minWidth:150,
                      rules: [{
                          required: true,
                          message: "请输入网关IMEI",
                          trigger: "blur"
                      }]
                  },
                  {
                      label: "供应商编码",
                      prop: "supplierSn",
                      minWidth:100,
                      rules: [{
                          required: true,
                          message: "请输入供应商编码",
                          trigger: "blur"
                      }]
                  },
                  {
                      label: "设备编码",
                      prop: "deviceSn",
                      search: true,
                      minWidth:110,
                      rules: [{
                          required: true,
                          message: "请输入设备编码",
                          trigger: "blur"
                      }]
                  },
                  {
                      label: "NB信号质量",
                      minWidth:110,
                      prop: "signQuality",
                      rules: [{
                          required: true,
                          message: "请输入NB信号质量",
                          trigger: "blur"
                      }]
                  },
                  {
                      label: "设备型号",
                      prop: "deviceMode",
                      hide: true,
                      rules: [{
                          required: true,
                          message: "请输入设备型号",
                          trigger: "blur"
                      }]
                  },
                  {
                      label: "电池串数",
                      prop: "batteryModeSeries",
                      hide: true,
                      rules: [{
                          required: true,
                          message: "请输入电池型号-电池串数",
                          trigger: "blur"
                      }]
                  },
                  {
                      label: "电池类型",
                      prop: "batteryModeType",
                      hide: true,
                      rules: [{
                          required: true,
                          message: "请输入电池型号-电池类型",
                          trigger: "blur"
                      }]
                  },
                  {
                      label: "电池电压",
                      prop: "batteryVoltage",
                      hide: true,
                      rules: [{
                          required: true,
                          message: "请输入电池电压",
                          trigger: "blur"
                      }]
                  },
                  {
                      label: "光板电压",
                      prop: "lightPlateVoltage",
                      hide: true,
                      rules: [{
                          required: true,
                          message: "请输入光板电压",
                          trigger: "blur"
                      }]
                  },
                  {
                      label: "光板电流",
                      prop: "lightPlateElectric",
                      hide: true,
                      rules: [{
                          required: true,
                          message: "请输入光板电流",
                          trigger: "blur"
                      }]
                  },
                  {
                      label: "负载电压",
                      prop: "loadVoltage",
                      hide: true,
                      rules: [{
                          required: true,
                          message: "请输入负载电压",
                          trigger: "blur"
                      }]
                  },
                  {
                      label: "负载电流",
                      prop: "loadElectric",
                      hide: true,
                      rules: [{
                          required: true,
                          message: "请输入负载电流",
                          trigger: "blur"
                      }]
                  },
                  {
                      label: "传感器类型",
                      prop: "sensorEnablationType",
                      hide: true,
                      minWidth:110,
                      rules: [{
                          required: true,
                          message: "请输入传感器使能-传感器类型",
                          trigger: "blur"
                      }]
                  },
                  {
                      label: "是否感应到人",
                      minWidth:120,
                      prop: "sensorEnablationHas",
                      hide: true,
                      rules: [{
                          required: true,
                          message: "请输入传感器使能-感应到人:1，没有人:0",
                          trigger: "blur"
                      }]
                  },
                  {
                      label: "充电温度",
                      prop: "chargingTemperature",
                      hide: true,
                      rules: [{
                          required: true,
                          message: "请输入充电温度",
                          trigger: "blur"
                      }]
                  },
                  {
                      label: "放电温度",
                      prop: "dischargeTemperature",
                      hide: true,
                      rules: [{
                          required: true,
                          message: "请输入放电温度",
                          trigger: "blur"
                      }]
                  },
                  {
                      label: "工作温度状态",
                      minWidth:120,
                      prop: "workingTemperatureState",
                      hide: true,
                      rules: [{
                          required: true,
                          message: "请输入工作温度状态",
                          trigger: "blur"
                      }]
                  },
                  {
                      label: "无线开关状态",
                      minWidth:120,
                      prop: "wirelessSwitchState",
                      hide: true,
                      rules: [{
                          required: true,
                          message: "请输入无线开关状态",
                          trigger: "blur"
                      }]
                  },
                  {
                      label: "运行时间",
                      prop: "runTime",
                      rules: [{
                          required: true,
                          message: "请输入运行时间",
                          trigger: "blur"
                      }]
                  },
                  {
                      label: "断电次数",
                      prop: "powerBreakNum",
                      rules: [{
                          required: true,
                          message: "请输入断电次数",
                          trigger: "blur"
                      }]
                  },
                  {
                      label: "模式修改次数",
                      prop: "patternUpdateNum",
                      minWidth:130,
                      hide: true,
                      rules: [{
                          required: true,
                          message: "请输入模式修改次数",
                          trigger: "blur"
                      }]
                  },
                  {
                      label: "日充电量",
                      prop: "dayCharge",
                      rules: [{
                          required: true,
                          message: "请输入日充电量",
                          trigger: "blur"
                      }]
                  },
                  {
                      label: "周充电量",
                      prop: "weekCharge",
                      hide: true,
                      rules: [{
                          required: true,
                          message: "请输入周充电量",
                          trigger: "blur"
                      }]
                  },
                  {
                      label: "月充电量",
                      prop: "monthCharge",
                      hide: true,
                      rules: [{
                          required: true,
                          message: "请输入月充电量",
                          trigger: "blur"
                      }]
                  },
                  {
                      label: "日放电量",
                      prop: "dayDischarge",
                      rules: [{
                          required: true,
                          message: "请输入日放电量",
                          trigger: "blur"
                      }]
                  },
                  {
                      label: "周放电量",
                      prop: "weekDischarge",
                      hide: true,
                      rules: [{
                          required: true,
                          message: "请输入周放电量",
                          trigger: "blur"
                      }]
                  },
                  {
                      label: "月放电量",
                      prop: "monthDischarge",
                      hide: true,
                      rules: [{
                          required: true,
                          message: "请输入月放电量",
                          trigger: "blur"
                      }]
                  },
                  {
                      label: "日过放次数",
                      prop: "dayOverDischarge",
                      hide: true,
                      minWidth:120,
                      rules: [{
                          required: true,
                          message: "请输入日过放次数",
                          trigger: "blur"
                      }]
                  },
                  {
                      label: "周过放次数",
                      prop: "weekOverDischarge",
                      hide: true,
                      minWidth:120,
                      rules: [{
                          required: true,
                          message: "请输入周过放次数",
                          trigger: "blur"
                      }]
                  },
                  {
                      label: "月过放次数",
                      prop: "monthOverDischarge",
                      hide: true,
                      minWidth:120,
                      rules: [{
                          required: true,
                          message: "请输入月过放次数",
                          trigger: "blur"
                      }]
                  },
                  {
                      label: "时间戳",
                      prop: "time",
                      type: "datetime",
                      minWidth:160,
                      rules: [{
                          required: true,
                          message: "请输入时间戳",
                          trigger: "blur"
                      }]
                  },
                  /*{
                      label: "路段",
                      prop: "roadSection",
                      rules: [{
                          required: true,
                          message: "请输入路段id",
                          trigger: "blur"
                      }]
                  },*/
              ]
          },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.devicedata_add, false),
          viewBtn: this.vaildData(this.permission.devicedata_view, false),
          delBtn: this.vaildData(this.permission.devicedata_delete, false),
          editBtn: this.vaildData(this.permission.devicedata_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      rowSave(row, loading, done) {
        add(row).then(() => {
          loading();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          done();
          console.log(error);
        });
      },
      rowUpdate(row, index, loading, done) {
        update(row).then(() => {
          loading();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          done();
          console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
</style>
